<?php
/**
 *
 *----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by PhpStorm.
 * User: huangxh
 * Date: 2018/6/15
 * Time: 下午 3:11
 */

use models\DAO\GroupModel;
use models\DAO\RuleModel;
use \Common\Common;
use Yaf\Registry;
class RuleController extends BaseController
{
    public function indexAction()
    {
        $this->display('index');
    }

    /**
     * 列表数据
     * @throws ReflectionException
     * @throws \Yaf\Exception
     */
    public function dataAction(){
        $ruleModel = RuleModel::getInstance();
        $condition = [];
        if (!empty(Registry::get('parameters')['searchText'])) $condition = ['title' =>Registry::get("parameters")['searchText']];
        $sort = !empty(Registry::get('parameters')['sort'])? Registry::get("parameters")['sort']:'id';
        list($rules) = $ruleModel->getList(
            $condition,Registry::get("parameters")['pageNumber'],Registry::get("parameters")['pageSize'],
            [$sort=>Registry::get("parameters")['sortOrder']]
        );

        $rules = Common::getTree($rules);
        foreach($rules as $key => $value){
            if ($value['level'] ==1)
                $rules[$key]['title'] =  str_repeat('├─', $value['level']).$value['title'];
            else
                $rules[$key]['title'] =  str_repeat('─', $value['level']).$value['title'];
        }

        $this->returnJson(['rows'=>$rules],false);
    }

    /**
     * 删除规则
     * @throws Exception
     * @throws \Yaf\Exception
     */
    public function ruleDelAction(){
        $ruleModel = RuleModel::getInstance();
        $result = $ruleModel->deleteData(Registry::get("parameters")['id']);
        if ($result instanceof Exception) throw $result;
        $this->returnJson($result);
    }

    /**
     * 添加规则
     * @throws Exception
     * @throws \Yaf\Exception
     */
    public function ruleAddAction()
    {

        $ruleModel = RuleModel::getInstance();
        $data = [
            'title' => Registry::get("parameters")['title'],
            'status'  =>empty(Registry::get("parameters")['status'])?0:Registry::get("parameters")['status'],
            'name'  =>Registry::get("parameters")['name'],
            'typeid'  =>Registry::get("parameters")['tpyeid'],
            'display'  =>empty(Registry::get("parameters")['display'])?0:Registry::get("parameters")['display'],
            'icon'  =>empty(Registry::get("parameters")['icon'])?'':Registry::get("parameters")['icon']
        ];
//        var_dump($data);exit;
        $result =$ruleModel->addData($data);
        if ($result instanceof Exception) throw $result;
        $this->returnJson($result);
    }

    /**
     * 修改菜单
     * @throws Exception
     * @throws \Yaf\Exception
     */
    public function ruleEditAction()
    {
        $ruleModel = RuleModel::getInstance();
        $condition = [
            'id' => Registry::get("parameters")['id'],
            'title' => Registry::get("parameters")['title'],
            'status'  =>Registry::get("parameters")['status']
        ];
        $result = $ruleModel->updateData($condition);
        if ($result instanceof Exception) throw $result;
        $this->returnJson($result,'',001);
    }

    /**
     * 生成权限树
     * @throws ReflectionException
     * @throws \Yaf\Exception
     */
    public function treeAction(){

        $rule = RuleModel::getInstance();

        $group = GroupModel::getInstance();
        $rules = $group->findOneBy(['id'=>Registry::get('parameters')['id']],'rules');//查询该账号所能访问的权限
        $rules = explode(',',$rules->_rules);

        $result = $rule->getTreeData();
        $genTree= Common::getTree($result);
        $tree = [];
        $f = -1;
        foreach($genTree as $value){
            if ($value['level'] ==0){
                $state = in_array($value['id'],$rules)?[['text'=>$value['title'],'id'=>$value['id'],'state'=>['checked'=>true,'selected'=>true]]]:[['text'=>$value['title'],'id'=>$value['id']]];

                $tree = array_merge($tree,$state);
                $tree[0]['nodes'] = [];
            }elseif($value['level'] == 1){
                $state1 = in_array($value['id'],$rules)?['text'=>$value['title'],'id'=>$value['id'],'url' => $value['name'],'state'=>['checked'=>true,'selected'=>true]]:['text'=>$value['title'],'id'=>$value['id'],'url' => $value['name']];
                array_push($tree[0]['nodes'],$state1);
                $f++;
                $tree[0]['nodes'][$f]['nodes'] = [];
            }else{
                $state2 = in_array($value['id'],$rules)?['text'=>$value['title'],'id'=>$value['id'],'url' => $value['name'],'state'=>['checked'=>true,'selected'=>true]]:['text'=>$value['title'],'id'=>$value['id'],'url' => $value['name']];
                array_push($tree[0]['nodes'][$f]['nodes'],$state2);
            }
        }


        $this->returnJson($tree,false);
    }

    /**
     * 菜单树
     * @throws ReflectionException
     * @throws \Yaf\Exception
     */
    public function ruleListAction(){
        $ruleModel = RuleModel::getInstance();
        list($rules) = $ruleModel->getList(['display'=>1],Registry::get("parameters")['pageNumber'],Registry::get("parameters")['pageSize']);
        $rules = Common::getTree($rules);
        foreach($rules as $key => $value){
            if ($value['level'] ==1)
                $rules[$key]['title'] =  str_repeat('├─', $value['level']).$value['title'];
            else
                $rules[$key]['title'] =  str_repeat('─', $value['level']).$value['title'];
        }
        $this->returnJson(['rows'=>$rules],false);
    }
}